class Note < ActiveRecord::Base
	belongs_to :note_type
	belongs_to :ticket
	validates_length_of :svalue, :maximum => 255
	validates_date :dvalue, :allow_nil => true, :if => Proc.new{ |u| (u.note_type.vtype == "vdate")}
        attr_protected :svalue, :bvalue, :tvalue, :dvalue
	
  def value
    case self.note_type.vtype
    when "vstring", "vselect"
      return self.svalue
    when "vcheckbox"
      return self.bvalue
    when "vtext"
      return self.tvalue
    when "vdate"
      return self.dvalue
    else
      nil
    end
  end
  
  def value=(val)
    case self.note_type.vtype
    when "vstring", "vselect" 
      self.svalue = val
    when "vcheckbox"
      self.bvalue = val
    when "vtext"
      self.tvalue = val
    when "vdate"
      self.dvalue = val
    end
  end
  
  def empty?
    if self.note_type.vtype == 'vcheckbox'
      return !self.bvalue
    end
    if ["vstring", "vselect"].include? self.note_type.vtype
      return self.svalue == '' || (self.svalue == nil)
    end
    if self.note_type.vtype == 'vtext'
      return (self.tvalue == "") || (self.tvalue == nil)
    end
    if  "vdate" == self.note_type.vtype
      return nil == self.dvalue
    end
    true
  end
  
	
end
